package com.nari.sink.service;

import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.nari.baseinfo.dao.IBuildingDao;
import com.nari.baseinfo.dao.ICommunityDao;
import com.nari.baseinfo.dao.IFileMappingDao;
import com.nari.baseinfo.dao.IHeatRoomDao;
import com.nari.coreframe.service.GenericServiceImpl;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResponseMsg;
import com.nari.platform.model.ResultWithPageCond;
import com.nari.sink.bo.PSinkInfo;
import com.nari.sink.dao.ISinkDao;

public class SinkBiz extends GenericServiceImpl<PSinkInfo, String> implements ISinkBiz {
	private String diskFilePath;
	private String sinkAttachFolder;
	private IHeatRoomDao iHeatRoomDao;
	private IBuildingDao iBuildingDao;
	private ICommunityDao iCommunityDao;
	private IFileMappingDao iFileMappingDao;

	/**
	 * 分页查询散热器列表
	 * @param sinkNo 散热器编号(可选)
	 * @param projectUID 所属项目(可选)
	 * @param communityUID 所属小区(可选)
	 * @param buildingUID 所属楼栋(可选)
	 * @param roomUID 所属热用户(可选)
	 * @param pageCond
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-28
	 */
	public ResultWithPageCond<PSinkInfo> queryHeatSinkPage(String sinkNo,
			String projectUID, String communityUID, String buildingUID, 
			String roomUID , PageCond pageCond) throws Exception {
		ISinkDao iSinkDao = (ISinkDao) this.getiGenericDao();
		return iSinkDao.queryHeatSinkPage(sinkNo, projectUID, communityUID, buildingUID, roomUID, pageCond);
	}

	/**
	 * 通过所属关系（所属项目、所属小区、所属楼栋、所属热用户）查询散热器列表
	 * 若所属关系均不存在，则返回空列表
	 * @param projectUID 所属项目(可选)
	 * @param communityUID 所属小区(可选)
	 * @param buildingUID 所属楼栋(可选)
	 * @param roomUID 所属热用户(可选)
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-28
	 */
	public List<PSinkInfo> querySinkerListByBelongs(String projectUID, String communityUID, 
			String buildingUID, String roomUID) throws Exception {
		ISinkDao iSinkDao = (ISinkDao) this.getiGenericDao();
		return iSinkDao.querySinkerListByBelongs(projectUID, communityUID, buildingUID, roomUID);
	}
	
	/**
	 * 带附件上传的散热器信息保存及修改
	 * @param heatSinker 散热器信息实体
	 * @param attachFile 散热器附件
	 * @param fileName 附件文件名
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-28
	 */
	public ResponseMsg saveOrUpdateHeatSinker(PSinkInfo heatSinker, File attachFile, 
			String fileName) throws Exception {
		//计算散热器功率修正系数，功率修正系数Kq = 散热器单片功率 * 散热器片数
		double kqFactor = new BigDecimal(heatSinker.getCalPower())
				.multiply(new BigDecimal(heatSinker.getPieceNum()))
				.doubleValue();
		heatSinker.setKqFactor(kqFactor);
		//保存散热器信息
		if(heatSinker.getId()!=null && !"".equals(heatSinker.getId())) {
			this.getiGenericDao().updateEntity(heatSinker, heatSinker.getId());
			//删除散热器原附件
			if(heatSinker.getPictureNo()!=null && !"".equals(heatSinker.getPictureNo())) {
				this.iFileMappingDao.deleteFileMapByUID(heatSinker.getPictureNo());
			}
		} else {
			this.getiGenericDao().saveEntity(heatSinker);
		}
		ResponseMsg rmsg = ResponseMsg.getInstance(true, "");
		//散热器照片附件管理
		if(attachFile != null) {
			rmsg = this.iFileMappingDao.saveAttachmentUpload(attachFile, fileName, diskFilePath, sinkAttachFolder);
			if(rmsg.isSuccess()) {
				heatSinker.setPictureNo(rmsg.getMsg());
				this.getiGenericDao().updateEntity(heatSinker, heatSinker.getId());
			}
		}
		return rmsg;
	}
	
	/**
	 * 删除散热器照片附件
	 * @param buildingUID
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-11
	 */
	public ResponseMsg deleteSinkerAttachFile(String heatSinkerUID) throws Exception {
		PSinkInfo heatSinker = this.getiGenericDao().queryEntityById(heatSinkerUID);
		//删除文件附件
		this.iFileMappingDao.deleteFileMapByUID(heatSinker.getPictureNo());
		//更新散热器信息
		heatSinker.setPictureNo(null);
		this.getiGenericDao().updateEntity(heatSinker, heatSinkerUID);
		return ResponseMsg.getInstance(true, "");
	}
	
	/**
	 * 通过散热器唯一标识删除散热器信息，并删除对应的散热器照片附件
	 * @param buildingUID
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-12
	 */
	public ResponseMsg deleteHeatSinkerByUID(String heatSinkerUID) throws Exception {
		PSinkInfo heatSinker = this.getiGenericDao().queryEntityById(heatSinkerUID);
		//删除散热器附件
		if(heatSinker.getPictureNo()!=null && !"".equals(heatSinker.getPictureNo())) {
			this.iFileMappingDao.deleteFileMapByUID(heatSinker.getPictureNo());
		}
		//删除散热器信息
		this.getiGenericDao().deleteEntityById(heatSinkerUID);
		return ResponseMsg.getInstance(true, "");
	}
	
	/**
	 * 通过热用户散热器列表查询与其对应的热用户id-houseNo映射表
	 * @param heatSinkerList
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-19
	 */
	public Map<String, String> queryReferHeatRoomMap(List<PSinkInfo> heatSinkerList) throws Exception {
		List<String> heatRoomIDList = null;
		if(heatSinkerList!=null && heatSinkerList.size()>0) {
			heatRoomIDList = new ArrayList<String>();
			for(PSinkInfo heatSink: heatSinkerList) {
				heatRoomIDList.add(heatSink.getRoomId());
			}
		}
		return this.iHeatRoomDao.queryIdNameMapByIDList(heatRoomIDList);
	}
	
	/**
	 * 通过热用户散热器列表查询与其对应的小区id-communityName映射表
	 * @param heatSinkerList
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-20
	 */
	public Map<String, String> queryReferCommunityMap(List<PSinkInfo> heatSinkerList) throws Exception {
		List<String> communityIDList = null;
		if(heatSinkerList!=null && heatSinkerList.size()>0) {
			communityIDList = new ArrayList<String>();
			for(PSinkInfo heatSink: heatSinkerList) {
				communityIDList.add(heatSink.getCommunityID());
			}
		}
		return this.iCommunityDao.queryIdNameMapByIDList(communityIDList);
	}
	
	/**
	 * 通过热用户散热器列表查询与其对应的楼栋id-buildingName映射表
	 * @param heatSinkerList
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-20
	 */
	public Map<String, String> queryReferBuildingMap(List<PSinkInfo> heatSinkerList) throws Exception {
		List<String> buildingIDList = null;
		if(heatSinkerList!=null && heatSinkerList.size()>0) {
			buildingIDList = new ArrayList<String>();
			for(PSinkInfo heatSink: heatSinkerList) {
				buildingIDList.add(heatSink.getBuildingID());
			}
		}
		return this.iBuildingDao.queryIdNameMapByIDList(buildingIDList);
	}
	
	/**
	 * 根据所属关系查询可绑定分配表的散热器列表
	 * 散热器与分配表的绑定原则是一一对应
	 * @param projectUID 所属项目(可选)
	 * @param communityUID 所属小区(可选)
	 * @param buildingUID 所属楼栋(可选)
	 * @param roomUID 所属热用户(可选)
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-29
	 */
	public List<PSinkInfo> queryBindableSinkerByBelongs(String projectUID, String communityUID, 
			String buildingUID, String roomUID) throws Exception {
		ISinkDao iSinkDao = (ISinkDao) this.getiGenericDao();
		return iSinkDao.queryBindableSinkerByBelongs(projectUID, communityUID, buildingUID, roomUID);
	}
	
	/**
	 * 根据所属热用户信息查询散热器列表
	 * @param sinkNo 散热器编号(可选)
	 * @param projectUID 所属项目(可选)
	 * @param communityUID 所属小区(可选)
	 * @param buildingUID 所属楼栋(可选)
	 * @param heatRoomUID 所属热用户(必填)
	 * 说明：若热用户ID不存在，则返回零结果集
	 * @param pageCond
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-9-4
	 */
	public ResultWithPageCond<PSinkInfo> querySinkerPageByRoomUID(String sinkNo,
			String projectUID, String communityUID, String buildingUID, 
			String heatRoomUID , PageCond pageCond) throws Exception {
		ISinkDao iSinkDao = (ISinkDao) this.getiGenericDao();
		return iSinkDao.querySinkerPageByRoomUID(sinkNo, projectUID, communityUID, buildingUID, heatRoomUID, pageCond);
	}
	
	/**
	 * 通过所属关系（所属项目、所属小区、所属楼栋、所属热用户）统计散热器的个数
	 * 若所属关系均不存在，则返回空列表
	 * @param projectUID 所属项目(可选)
	 * @param communityUID 所属小区(可选)
	 * @param buildingUID 所属楼栋(可选)
	 * @param roomUID 所属热用户(可选)
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-10-23
	 */
	public int countSinkerSumByBelongs(String projectUID, String communityUID, 
			String buildingUID, String roomUID) throws Exception {
		ISinkDao iSinkDao = (ISinkDao) this.getiGenericDao();
		return iSinkDao.countSinkerSumByBelongs(projectUID, communityUID, buildingUID, roomUID);
	}
	
	/**
	 * 统计相同散热器编号的数量，避免系统录入相同散热器编号的散热器信息
	 * @param sinkNo
	 * @param sinkerUID
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-7-28
	 */
	public int countBySinkNo(String sinkNo, String sinkerUID) throws Exception {
		ISinkDao iSinkDao = (ISinkDao) this.getiGenericDao();
		return iSinkDao.countBySinkNo(sinkNo, sinkerUID);
	}

	/**
	 * @return the diskFilePath
	 */
	public String getDiskFilePath() {
		return diskFilePath;
	}

	/**
	 * @param diskFilePath the diskFilePath to set
	 */
	public void setDiskFilePath(String diskFilePath) {
		this.diskFilePath = diskFilePath;
	}

	/**
	 * @return the sinkAttachFolder
	 */
	public String getSinkAttachFolder() {
		return sinkAttachFolder;
	}

	/**
	 * @param sinkAttachFolder the sinkAttachFolder to set
	 */
	public void setSinkAttachFolder(String sinkAttachFolder) {
		this.sinkAttachFolder = sinkAttachFolder;
	}

	/**
	 * @return the iHeatRoomDao
	 */
	public IHeatRoomDao getiHeatRoomDao() {
		return iHeatRoomDao;
	}

	/**
	 * @param iHeatRoomDao the iHeatRoomDao to set
	 */
	public void setiHeatRoomDao(IHeatRoomDao iHeatRoomDao) {
		this.iHeatRoomDao = iHeatRoomDao;
	}

	/**
	 * @return the iBuildingDao
	 */
	public IBuildingDao getiBuildingDao() {
		return iBuildingDao;
	}

	/**
	 * @param iBuildingDao the iBuildingDao to set
	 */
	public void setiBuildingDao(IBuildingDao iBuildingDao) {
		this.iBuildingDao = iBuildingDao;
	}

	/**
	 * @return the iCommunityDao
	 */
	public ICommunityDao getiCommunityDao() {
		return iCommunityDao;
	}

	/**
	 * @param iCommunityDao the iCommunityDao to set
	 */
	public void setiCommunityDao(ICommunityDao iCommunityDao) {
		this.iCommunityDao = iCommunityDao;
	}

	/**
	 * @return the iFileMappingDao
	 */
	public IFileMappingDao getiFileMappingDao() {
		return iFileMappingDao;
	}

	/**
	 * @param iFileMappingDao the iFileMappingDao to set
	 */
	public void setiFileMappingDao(IFileMappingDao iFileMappingDao) {
		this.iFileMappingDao = iFileMappingDao;
	}
	
}
